package Thread;

import java.util.Scanner;

public class Demo21 {
    //内存可见性，由于编译器优化
    //volatile修饰的变量，此时编译器对这个变量进行读取操作，不会被优化成寄存器
    private volatile static int flag = 0;
    public static void main(String[] args) {
        Thread t1 = new Thread(()->{
            while(flag==0){}
            System.out.println("t1 end");
        });
        Thread t2 = new Thread(()->{
            Scanner sc = new Scanner(System.in);
            flag=sc.nextInt();
        });
        t1.start();
        t2.start();
    }
}
